import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/evaluation.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonsTeacherSwitchClass.dart';
import 'package:ichinesereader/widgets/grids/evaluationRecordDataGrid.dart';

class EvaluationRecordsForTeacher extends StatefulWidget {
  static const String routeName = '/teacher/evaluation/records';

  @override
  State<EvaluationRecordsForTeacher> createState() =>
      _EvaluationRecordsForTeacherState();
}

class _EvaluationRecordsForTeacherState
    extends State<EvaluationRecordsForTeacher> {
  final box = Hive.box('appconfig');
  final EvaluationController controller = Get.find();

  @override
  void initState() {
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    var classId = box.get(CacheManagerKey['currentclass'].toString());

    Future.delayed(Duration(milliseconds: 200), (() {
      if (isTeacher) {
        controller.fetchEvaluationRecords('0', '0', '', classId);
      }
    }));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher evaluation records');

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: controller.obx(
        (data) =>
            EvaluationRecordDataGrids(controller.evaluationRecordItems, true),
        onEmpty:
            EvaluationRecordDataGrids(controller.evaluationRecordItems, true),
        onLoading: Container(),
      ),
      floatingActionButton: FloatingActionButtonsTeacherSwitchClass(),
    );
  }
}
